﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace MineCraft_Command_Editor.Project
{
    class CreateBadRockEdition
    {
        internal static void CreateProject(string path)
        {
            
        }

        /// <summary>
        /// 组装manifest文件
        /// </summary>
        /// <param name="version">指定版本</param>
        /// <param name="lowestVersion">最低版本</param>
        /// <param name="projectName">工程名称</param>
        /// <param name="n">U-UID</param>
        /// <returns></returns>
        internal static string CreateManifest(string version,string lowestVersion,string projectName,List<string> nn)
        {
            /*
               {
                "format_version": 1,
                "header": {
                    "description": "Function worldedit",
                    "name": "Function worldedit",
                    "uuid": "11111111-1111-1111-1111-111111111111",
                    "version": [1, 8, 1],
                    "min_engine_version": [1, 8, 0]
                },
                "modules": [
                    {
                        "description": "",
                        "type": "data",
                        "uuid": "22222222-2222-2222-2222-222222222222",
                        "version": [1, 8, 1]
                    }
                ]
            }
             */
            StringBuilder sb = new StringBuilder();

            //组装UUID
            string n = CreateUUid(nn[0], nn[1], nn[2], nn[3]);

            //组装版本号
            version = CreateVersion(version);
            lowestVersion = CreateVersion(lowestVersion);

            //插入各项
            sb.Append("{\n");
            sb.Append("\"format_version\": 1,\n");
            sb.Append("\"header\": { \n");
            sb.Append($"    \"description\": \"{projectName}\",\n");
            sb.Append($"    \"name\": \"{projectName}\",\n");
            sb.Append($"    \"uuid\": \"{n}\",\n");
            sb.Append($"    \"version\": {version},\n");
            sb.Append($"    \"min_engine_version\": {lowestVersion},\n");
            sb.Append("},\n");
            sb.Append("\"modules\": [\n");
            sb.Append("   {\n");
            sb.Append("    \"description\": \"\",\n");
            sb.Append("    \"type\": \"data\",\n");
            sb.Append($"    \"uuid\": \"{n}\",\n");
            sb.Append($"    \"version\": {version},\n");
            sb.Append("       }\n");
            sb.Append("    ]\n");
            sb.Append("}");

            return sb.ToString();
        }

        /// <summary>
        /// 组装U-UID
        /// </summary>
        /// <param name="t1">Ln1</param>
        /// <param name="t2">Ln2</param>
        /// <param name="t3">Ln3</param>
        /// <param name="t4">Ln4</param>
        /// <returns></returns>
        protected static string CreateUUid(string t1,string t2,string t3,string t4)
        {
            return t1 + "-" + t2 + "-" + t3 + "-" + t4;
        }

        /// <summary>
        /// 组装版本号
        /// </summary>
        /// <param name="version">版本号</param>
        /// <returns></returns>
        protected static string CreateVersion(string version)
        {
            try
            {
                string[] temp = version.Split('.');
                string result = $"[{temp[0]},{temp[1]},{temp[2]}]";
                return result;
            }
            catch
            {
                //补零法
                string[] temp = version.Split('.');
                string result = $"[{temp[0]},{temp[1]},0]";
                return result;
            }
        }

        internal static void CreateFileSystem(string path, string version, string lowestVersion, string projectName, List<string> nn,string nm,string pt)
        {
            //寻找文件名
        
            //建立一般文件系统
            path = CreatePath(path);
            Directory.CreateDirectory(path + "\\behavior_packs");
            Directory.CreateDirectory(path + "\\behavior_packs\\functions");
            Directory.CreateDirectory(path + "\\behavior_packs\\entities");
            StreamWriter sw = new StreamWriter(path + "\\behavior_packs\\manifest.txt");
            sw.Write((CreateManifest(version, lowestVersion, projectName, nn)));
            sw.Close();

            //建立解决方案
            List<string> cs = CreateCS(CreatePath(path), version, lowestVersion, nm, CreateUUid(nn[0], nn[1], nn[2], nn[3]),pt);
            CreateXmlTree(path + "\\", cs);
        }

        protected static string CreatePath(string path)
        {
            string[] pathInfo = path.Split('\\');
            //删掉文件名
            pathInfo[pathInfo.Length - 1] = null;

            //重排
            string result = "";

            foreach (var item in pathInfo)
            {
                if (item != null)
                {
                    result += item;
                    result += "\\";
                }
            }

            return result;
        }

        public static List<string> CreateCS(string path, string ver, string lver, string nm, string uid,string jj)
        {

            List<string> cs = new List<string>
            {
                path,
                ver,
                lver,
                nm,
                uid,
                jj,
            };

            return cs;

        }

        public static void CreateXmlTree(string xmlPath,List<string> cs)
        {
            /*参数:
             * 绝对路径
             * 目标版本号
             * 最低版本号
             * 工程名
             * U-UID
             * 解决方案名称
             */
            XElement xElement = new XElement(
                new XElement("Project",

                        new XElement("Path",cs[0]),
                        new XElement("Edition", "BadRock"),
                        new XElement("Version", cs[1]),
                        new XElement("LowestVersion", cs[2]),
                        new XElement("ProjectName", cs[3]),
                        new XElement("U-UID", cs[4]),
                        new XElement("CreateTime", DateTime.Now.ToString("yyyy-MM-dd"))
         
                        )
                );

            //需要指定编码格式，否则在读取时会抛：根级别上的数据无效。 第 1 行 位置 1异常
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Encoding = new UTF8Encoding(false);
            settings.Indent = true;
            XmlWriter xw = XmlWriter.Create(cs[5], settings);
            xElement.Save(xw);
            //写入文件
            xw.Flush();
            xw.Close();
        }
    }
}
